package com.boss.salary.controller;


import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.SalaryMonthEnd;
import com.boss.salary.service.ISalaryMonthEndService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salaryMonthEndController", produces = MediaType.APPLICATION_JSON)
public class SalaryMonthEndController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryMonthEndController.class);

    @Autowired
    private ISalaryMonthEndService salaryMonthEndService;

    /**
     * 十三月工资：保存
     * @param list
     * @return
     */
    @PostMapping("/saveThirteenthMonthSalary")
    public ApiResultDTO saveThirteenthMonthSalary(@RequestBody List<SalaryMonthEnd> list){
        try{
            salaryMonthEndService.saveThirteenthMonthSalary(list);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.warn(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "保存失败！");
        }
    }

    /**
     * 十三月工资：查询十三月工资
     * @param agencyId
     * @return
     */
    @GetMapping("/queryThirteenSalaryPage")
    public ApiResultDTO queryThirteenSalaryPage(@RequestHeader("tokenid") String tokenid,
                                                @RequestParam("agencyId") String agencyId){
        try{
            UserDTO userDTO = getUser(tokenid);
            List<Map<String, Object>> page = salaryMonthEndService.getThirteenSalaryPage(userDTO,agencyId);
            return ApiResultDTO.success(page);
        }catch (Exception e){
            logger.warn(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }

    /**
     * 十三月工资：重新生成
      * @return
     */
    @PostMapping("/physicalDeleteThirteenMonthSalary/{agencyId}")
    public ApiResultDTO physicalDeleteThirteenMonthSalary(@PathVariable("agencyId") String agencyId){
        try{
            salaryMonthEndService.physicalDeleteThirteenMonthSalary(agencyId);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.warn(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "失败！");
        }
    }

    /**
     * 十三月工资：修改现有十三月工资
     * @param list
     * @return
     */
    @PostMapping("/submitThirteenthMonthSalary")
    public ApiResultDTO submitThirteenthMonthSalary(@RequestBody List<SalaryMonthEnd> list){
        try{
            salaryMonthEndService.submitThirteenthMonthSalary(list);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.warn(e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败！");
        }
    }

    /**
     * 获取实发工资
     * @param agencyId
     * @return
     */
    @GetMapping("/getPaidWagesCount")
    public Integer getPaidWagesCount(@RequestHeader("tokenid") String tokenid,String agencyId){
        try{
            UserDTO userDTO = getUser(tokenid);
            return salaryMonthEndService.getPaidWagesCount(agencyId,userDTO);
        }catch (Exception e){
            logger.warn(e.getMessage());
            return 10000;
        }
    }

    /**
     * 根据单位id获取未审核人数
     * @param agencyId
     * @return
     */
    @GetMapping("/getUnreviewedPersonCount")
    public Integer getUnreviewedPersonCount(String agencyId){
        try{
            return salaryMonthEndService.getUnreviewedPersonCount(agencyId);
        }catch (Exception e){
            logger.warn(e.getMessage());
            return 10000;
        }
    }
}
